package vip.zhenzicheng.design_patterns.responsibility.jdk;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhenzicheng
 * @date 2022-05-26 19:02
 */
public class FilterChain { //过滤器链

  private final List<Filter> filters = new ArrayList<>();

  private int index = 0;

  // 链式调用
  public FilterChain addFilter(Filter filter) {
    this.filters.add(filter);
    return this;
  }

  public void doFilter(Request request, Response response) {
    if (index == filters.size()) {
      return;
    }
    Filter filter = filters.get(index);
    index++;
    filter.doFilter(request, response, this);
  }
}
